package zen2d.example.lol;

import zen2d.*;
import zen2d.SwingWindow;
import zen2d.util.AssertUtil;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;


public class Manzi extends Sprite {

    int hp = 100;

    Direction direction;
    int speed = 5;
    Animation walk_R;
    Animation walk_L;
    Animation walk_U;
    Animation walk_D;
    Animation xf;

    Animation current;

    Animation attack_R;

   Attack attack = new Attack(x+width,y,150,100);


    public Manzi(int x, int y, int width, int height) {
        super(x, y, width, height);
        BufferedImage [][] bufferedImages = AssertUtil.split(AssertUtil.getImage("image/lol/manzi/manzi-walk.png"),8,8);

        walk_R = new Animation();
        walk_R.setDuration(100);
        walk_R.setKeyFrames(bufferedImages[6]);

        walk_L = new Animation();
        walk_L.setDuration(100);
        walk_L.setKeyFrames(bufferedImages[2]);

        walk_U = new Animation();
        walk_U.setDuration(100);
        walk_U.setKeyFrames(bufferedImages[4]);

        walk_D = new Animation();
        walk_D.setDuration(100);
        walk_D.setKeyFrames(bufferedImages[0]);


        xf = new Animation();
        xf.setDuration(50);
        xf.setKeyFrames(AssertUtil.parseOneDimensionalArray(AssertUtil.split(AssertUtil.getImage("image/lol/manzi/manzi-xf.png"),4,3)));
//        xf.setKeyFrames(AssertUtil.split(AssertUtil.getImage("/assert/image/lol/manzi/manzi-xf.png"),4,3)[0]);
        xf.setPlayMode(Animation.PlayMode.NORMAL);
        current = walk_R;
        direction = Direction.S;

        BufferedImage [][] bufferedImages1 = AssertUtil.split(AssertUtil.getImage("image/lol/manzi/manzi-attack-1.png"),4,4);
        attack_R = new Animation();
        attack_R.setKeyFrames(AssertUtil.concatAll(bufferedImages1[0],bufferedImages1[1]));
        attack_R.setDuration(100);
        attack_R.setPlayMode(Animation.PlayMode.NORMAL);

        Zen2d.addSprite(attack);
    }

    @Override
    protected void afterEndLive() {

    }

    @Override
    protected void update() {

        switch (direction){
            case L:{
                x -= speed;
            }break;
            case R:{
                x+=speed;
            }break;
            case U:{
                y -= speed;
            }break;
            case D:{
                y+= speed;
            }break;
            case S:{

            }break;
        }

        if (attack_R.isFinished()) {
            attack.endLive();
        }

        if(y >= 500){
            Zen2d.moveViewport(0,speed);
        }

    }

    @Override
    protected void draw(GraphicsContext graphicsContext) {


        int width = this.width;
        int height = this.height;
        if (current == attack_R){
            width = 350;
            height = 350;
        }
        graphicsContext.drawImage(current.getKeyFrame(),x,y,width,height);

        graphicsContext.setColor(Color.PINK);
        graphicsContext.drawRect(x,y,width,height,false);

        graphicsContext.drawString(String.valueOf(hp),x+width/2,y-20,30);

    }

    @Override
    public void keyDown(int keyCode) {
        super.keyDown(keyCode);

        switch (keyCode){
            case  KeyEvent.VK_W:{
                if (attack_R.isStart() && !attack_R.isFinished()){
                    return;
                }
                direction = Direction.U;current = walk_U;}break;
            case  KeyEvent.VK_A:{
                if (attack_R.isStart() && !attack_R.isFinished()){
                    return;
                }
                direction  = Direction.L;current = walk_L;}break;
            case  KeyEvent.VK_S:{
                if (attack_R.isStart() && !attack_R.isFinished()){
                    return;
                }
                direction = Direction.D;current = walk_D;}break;
            case  KeyEvent.VK_D:{
                if (attack_R.isStart() && !attack_R.isFinished()){
                    return;
                }
                direction = Direction.R;current = walk_R;}break;
        }

    }

    @Override
    protected void up() {

    }

    @Override
    protected void down() {

    }

    @Override
    protected void left() {

    }

    @Override
    protected void right() {

    }

    @Override
    protected void space() {

        if (attack_R.isStart() && !attack_R.isFinished()){
            return;
        }

        direction = Direction.S;
        attack_R.reset();
        current = attack_R;

        attack.setLocation(x+width,y+height/2);
//        Window.addSprite(attack);
        attack.show();

//        xf.reset();


    }
}
